package cn.hncu.booksManagementSystem.stock.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.stock.business.ebi.StockEbi;
import cn.hncu.booksManagementSystem.stock.business.factory.StockEbiFactory;
import cn.hncu.booksManagementSystem.stock.vo.StockModel;
import cn.hncu.booksManagementSystem.stock.vo.StockQueryModel;



public class ListPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private  JFrame mainFrame;
	private JList<StockModel> JList;
	//注入ebi
	private StockEbi stockEbi = StockEbiFactory.getStockEbi();
	private BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	private JTextField tfdId;
	private JTextField tfdSumNum;
	private JTextField tfdSumNum2;

	private JComboBox<String> cbbxBook;
	/**
	 * Create the panel.
	 * @wbp.parser.constructor
	 */
	public ListPanel( JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
		initJListData();
	}
	
	private void initJListData() {
		Collection<BookModel> books = bookEbi.getAll();
		cbbxBook.addItem("查询所有");
		for (BookModel book : books) {
			cbbxBook.addItem(book.getId()+" ,《"+book.getName()+"》");
		}
		JList.setListData(stockEbi.getAll().toArray(new StockModel[0]));
	}
	
	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("库存管理");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(308, 10, 145, 38);
		add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(107, 205, 561, 283);
		add(scrollPane);
		
		JList = new JList<StockModel>();
		scrollPane.setViewportView(JList);
		JList.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		JButton btnQuery = new JButton("查询...");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnQueryActionPerformed(e);
			}
		});
		btnQuery.setForeground(Color.BLUE);
		btnQuery.setFont(new Font("微软雅黑", Font.BOLD, 20));
		btnQuery.setBounds(650, 146, 101, 38);
		add(btnQuery);
		
		JLabel lblId = new JLabel("ID：");
		lblId.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		lblId.setBounds(107, 84, 45, 32);
		add(lblId);
		
		tfdId = new JTextField();
		tfdId.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		tfdId.setBounds(152, 83, 165, 35);
		add(tfdId);
		tfdId.setColumns(10);
		
		tfdSumNum = new JTextField();
		tfdSumNum.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		tfdSumNum.setColumns(10);
		tfdSumNum.setBounds(152, 149, 165, 35);
		add(tfdSumNum);
		
		JLabel label_1 = new JLabel("最少数量：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label_1.setBounds(58, 150, 95, 32);
		add(label_1);
		
		JLabel label_2 = new JLabel("图书：");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label_2.setBounds(365, 84, 54, 32);
		add(label_2);
		
		tfdSumNum2 = new JTextField();
		tfdSumNum2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		tfdSumNum2.setColumns(10);
		tfdSumNum2.setBounds(434, 149, 176, 35);
		add(tfdSumNum2);
		
		cbbxBook = new JComboBox<String>();
		cbbxBook.setToolTipText("");
		cbbxBook.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cbbxBook.setBounds(428, 86, 240, 32);
		add(cbbxBook);
		
		JLabel label_3 = new JLabel("最多数量：");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label_3.setBounds(327, 150, 90, 32);
		add(label_3);
	}
	/**
	 * 查询事件响应
	 * @param e 事件源
	 */
	protected void btnQueryActionPerformed(ActionEvent e) {
		//1收集参数(带格式校验) id,bookId,sumNum,sumNum2
		String id = tfdId.getText().trim();
		if(id!=null && id.length()>0) {
			if (!id.matches("^\\d{1,10}$")) {
				JOptionPane.showMessageDialog(this, "id只能有数字组成，且长度必须在1-10之间");
				return;
			}
		}
		
		String book = (String) cbbxBook.getSelectedItem();
		String bookId = null;
		if (book!=null && !"查询所有".equals(book)) {
			bookId = book.split(",")[0].trim();
		}
		
		int sumNum = 0;
		String strSumNum = tfdSumNum.getText().trim();
		if (strSumNum!=null && strSumNum.length()>0) {
			try {
				sumNum = Integer.parseInt(strSumNum);
			} catch (NumberFormatException e1) {
				JOptionPane.showMessageDialog(this, "最少数量格式错误！");
				return;
			}
		}
		
		int sumNum2 = 0;
		String strSumNum2 = tfdSumNum2.getText().trim();
		if (strSumNum2!=null && strSumNum2.length()>0) {
			try {
				sumNum2 = Integer.parseInt(strSumNum2);
			} catch (NumberFormatException e1) {
				JOptionPane.showMessageDialog(this, "最多数量格式错误！");
				return;
			}
		}
		//2组织参数
		StockQueryModel sqm = new StockQueryModel();
		sqm.setId(id);
		sqm.setBookId(bookId);
		sqm.setSumNum(sumNum);
		sqm.setSumNum2(sumNum2);
		//3调用逻辑层
		Collection<StockModel> resCol = stockEbi.getStocksByCondition(sqm);
		//4导向结果界面
		JList.setListData(resCol.toArray(new StockModel[0]));
		mainFrame.validate();
	}
}
